<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="text">
<input type="button" value="平方" onclick="f()">
<div></div>
<script>
    function f() {
        //取出文本框和div
        let i = document.querySelector("input");
        let d = document.querySelector("div");
        //判断用户输入的内容是否是NaN
        if (isNaN(i.value)){
            d.innerText = "输入错误!";
            return;
        }
        //把输入的内容平方后赋值给div    NaN*NaN
        //字符串在进行-*/运算时会自动转成数值,如果转不成数值会转成NaN
        //d.innerText = i.value*i.value;
        //如果需要进行加法运算
        // d.innerText = i.value*1+i.value*1;
        d.innerText = parseFloat(i.value)+parseFloat(i.value);
    }
</script>
</body>
</html>